package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: dw
 * @Date 2022/3/11 23:39
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired(required = false)
    private DishService dishService;


    /**
     * 根据条件分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page,Integer pageSize,String name){
        //调用业务层查找
        Page<DishDto> dtoPage = dishService.findByPage(page, pageSize, name);
        return R.success(dtoPage);
    }

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.save(dishDto);
        return R.success("添加成功");
    }

    /**
     * 根据id回显菜品
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.update(dishDto);
        return R.success("修改成功");
    }

    /**
     * 批量修改状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam("ids") List<Long> ids){
        dishService.updateStatus(status,ids);
        return R.success("修改成功");
    }

    /**
     * 根据菜系id展示所有菜品（套餐用）
     * @param categoryId
     * @return
     */
    @RequestMapping("/list")
    public R<List<DishDto>> findCategoryIdAndStatus(Long categoryId,Integer status){
        List<DishDto> dishDtoList = dishService.findCategoryIdAndStatus(categoryId, status);
        return R.success(dishDtoList);
    }
}

